<?php

class accel extends base {

    private $content;

    public function submenu() {
        $this->submenu = array();
        $this->submenu[0]["name"] = "Control";
        $this->submenu[0]["callback"] = "control";
        $this->submenu[0]["icon"] = "icons/cross.png";
        $this->submenu[0]["rights"] = 0;
        $this->submenu[0]["enabled"] = true;
        $this->submenu[1]["name"] = "Config";
        $this->submenu[1]["callback"] = "config";
        $this->submenu[1]["icon"] = "icons/cross.png";
        $this->submenu[1]["rights"] = 0;
        $this->submenu[1]["enabled"] = true;
        $this->_submenu($this->submenu);
    }

    public function run() {
        $this->submenu();
        $callback = $this->_run();
        if ($callback)
            $this->content = $this->$callback();
        $this->finish($this->content);
    }

    public function process_config($config) {
        $configa = explode("\n", $config);
        foreach ($configa as $ca) {
            if (strstr($ca, "["))
                $section = $ca;
            else {
                $caa = explode("=", $ca);
                $config_parsed[$section][$caa[0]] = $caa[1];
            }
        }
        return $config_parsed;
    }

    public function config() {
        $config = file_get_contents("/etc/accel-ppp.conf");
        $conf = $this->process_config($config);
        foreach ($conf as $section => $secvals) {
            $output.="<h1>{$section}</h1><br>\n";
            foreach ($secvals as $var => $val) {
                if (strstr($var, "#"))
                    $var = "<em>{$var}</em>";
                if ($val)
                    $output.="{$var} = {$val}<br>\n";
                else
                    $output.="{$var}<br>\n";
            }
        }
        return $output;
    }

    public function showmessage($message, $type) {
        $output.="<div class=\"flash\">
        <div class=\"message {$type}\">
        <p>{$message}</p>
        </div>
        </div>";
        return $output;
    }

    public function control() {
        $action = $_POST["action"];
        switch ($action) {
            case "terminateall":
                exec("graphs/accel-command.sh \"terminate all soft\"");
                $output.=$this->showmessage("All users terminated!", "warning");
                break;
            case "reload":
                exec("graphs/accel-command.sh \"reload\"");
                $output.=$this->showmessage("Settings reloaded!", "notice");
                break;
            case "restarthard":
                exec("graphs/accel-command.sh \"restart hard\"");
                $output.=$this->showmessage("Restarted hard!", "warning");
                break;
            case "restartgrace":
                exec("graphs/accel-command.sh \"restart gracefully\"");
                $output.=$this->showmessage("Restarted gracefully!", "warning");
                break;
            case "restartsoft":
                exec("graphs/accel-command.sh \"restart soft\"");
                $output.=$this->showmessage("Restarted soft!", "warning");
                break;
            case "shutdowncancel":
                exec("graphs/accel-command.sh \"shutdown cancel\"");
                $output.=$this->showmessage("Cancel shutdown", "warning");
                break;
            case "shutdownhard":
                exec("graphs/accel-command.sh \"shutdown hard\"");
                $output.=$this->showmessage("Shutdown hard", "warning");
                break;
            case "shutdownsoft":
                exec("graphs/accel-command.sh \"shutdown soft\"");
                $output.=$this->showmessage("Shutdown soft", "warning");
                break;
            case "shutdown":
                exec("graphs/accel-command.sh \"shutdown\"");
                $output.=$this->showmessage("Shutdown", "warning");
                break;
            case "restartservice":
                exec('sudo service accel-ppp restart');
                $output.=$this->showmessage("Restart", "notice");
                break;
            default:
        }
        $r = exec("pgrep accel-pppd");
        if ($r) {
            $output.="<img src='templates/sand/images/ok.gif'> Accel-PPPD process is running, PID" . $r . "<br>\n<br>\n";
            $status = file_get_contents("graphs/status");
            $statusa = explode("\r", $status);
            foreach ($statusa as $sa) {
                if (strstr($sa, "cpu") || strstr($sa, "uptime") || strstr($sa, "mem"))
                    $output.="{$sa}<br>\n";
            }

            $output.=<<<OUTPUT
        <h1>Shutdown</h1>
        <table>
        <tr>
        <td>
        <form method=POST>
        <input type="hidden" name="action" value="shutdown">
        <button class="button" type="submit"><img src="templates/sand/images/icons/bomb.gif" alt="Shutdown" /> Shutdown</button><br>
        </form>
        </td>
        <td>
        <form method=POST>
        <input type="hidden" name="action" value="shutdownsoft">
        <button class="button" type="submit"><img src="templates/sand/images/icons/bomb.gif" alt="Shutdown soft" /> Shutdown soft</button><br>
        </form>
        </td>
        <td>
        <form method=POST>
        <input type="hidden" name="action" value="shutdownhard">
        <button class="button" type="submit"><img src="templates/sand/images/icons/bomb.gif" alt="Shutdown hard" /> Shutdown hard</button><br>
        </form>
        </td>
        <td>
        <form method=POST>
        <input type="hidden" name="action" value="shutdowncancel">
        <button class="button" type="submit"><img src="templates/sand/images/icons/bomb.gif" alt="Shutdown cancel" /> Shutdown cancel</button><br>
        </form>
        </td>
        </tr>
        </table>
        <h1>Restart</h1>
        <table>
        <tr>
        <td>
        <form method=POST>
        <input type="hidden" name="action" value="restartsoft">
        <button class="button" type="submit"><img src="templates/sand/images/icons/excl.gif" alt="Restart daemon soft" /> Restart soft</button><br>
        </form>
        </td>
        <td>
        <form method=POST>
        <input type="hidden" name="action" value="restartgrace">
        <button class="button" type="submit"><img src="templates/sand/images/icons/excl.gif" alt="Restart daemon gracefully" /> Restart gracefully</button><br>
        </form>
        </td>
        <td>
        <form method=POST>
        <input type="hidden" name="action" value="restarthard">
        <button class="button" type="submit"><img src="templates/sand/images/icons/excl.gif" alt="Restart daemon hard" /> Restart hard</button><br>
        </form>
        </td>
        </tr>
        </table>
        <h1>Other</h1>
        <table>
        <tr>
        <td>
        <form method=POST>
        <input type="hidden" name="action" value="reload">
        <button class="button" type="submit"><img src="templates/sand/images/icons/key.png" alt="Reload config" /> Reload config</button><br>
        </form>
        </td>
        <td>
        <form method=POST>
        <input type="hidden" name="action" value="terminateall">
        <button class="button" type="submit"><img src="templates/sand/images/icons/forbidden.gif" alt="Terminate all sessions" /> Terminate all</button><br>
        </form>
        </td>
        </tr>
        <table>
OUTPUT;
        }
        else {
            $output.="<img src='templates/sand/images/excl.gif'> Accel-PPPD process is not running<br>\n<br>\n";
            $output.=<<<OUTPUT
            <h1>Shutdown</h1>
            <table>
            <tr>
            <td>
            <form method=POST>
            <input type="hidden" name="action" value="restartservice">
            <button class="button" type="submit"><img src="templates/sand/images/icons/bomb.gif" alt="Restart service" /> Restart service</button><br>
            </form>
            </td>
OUTPUT;
        }


        return $output;
    }

}

?>